#include <header.h>

int main(){

    int fd_r = open("1.pipe", O_RDONLY);
    int fd_w = open("2.pipe", O_WRONLY);

    while(1){

        fd_set set;
        FD_ZERO(&set);
        FD_SET(STDIN_FILENO, &set);
        FD_SET(fd_r, &set);

        // 最后一个参数: 超时时间NULL-> 一直等待, 直到监听的文件对象, 至少有一个满足条件 

        struct timeval tv;
        tv.tv_sec = 10;
        tv.tv_usec = 0;
        int ret = select(fd_r+1, &set, NULL, NULL, &tv);

        printf("time: %ld \n", tv.tv_sec);

        if(ret == 0){
            printf("超时 \n");
        }else{
            printf("就绪 \n");
        }

        if(FD_ISSET(STDIN_FILENO, &set)){
            char buf[100] = {0};
            read(STDIN_FILENO, buf, sizeof(buf));
            write(fd_w, buf, sizeof(buf));
        }
        if(FD_ISSET(fd_r, &set)){
            char buf[100] = {0};
            size_t ret = read(fd_r, buf, sizeof(buf));
            if(ret == 0){
                break;
            }
            printf("buf: %s \n", buf);
        }
    }

    close(fd_r);
    close(fd_w);
    return 0;
}

